package com.yellowpage.omc.datacollect.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;

import com.yellowpage.omc.datacollect.data.DataCollect;
import com.yellowpage.omc.datacollect.data.DataCollectBatch;
import com.yellowpage.omc.datacollect.data.DataVerify;
import com.yellowpage.omc.datacollect.service.DataCollectService;
import com.yellowpage.omc.datacollect.service.DataVerifyService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class DataCollectAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_APPROVED = "approved";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<DataCollect> dataCollectList = new ArrayList<DataCollect>();
	private List<Map> resultList = new ArrayList<Map>();

	private DataCollect dataCollect;
	private DataCollectService dataCollectService;

	private DataVerify dataVerify;
	private DataVerifyService dataVerifyService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_dataCollectId;
	private String query_dataCollectBatchId;
	private String query_batchNum;
	private String query_seq;
	private String query_link;
	private String query_telType;
	private String query_tel;
	private String query_city;
	private String query_class01;
	private String query_class02;
	private String query_addr;
	private String query_fax;
	private String query_tel02;
	private String query_createTime;
	private String query_lastDataVerifyId;
	private String query_lastVerifyTime;
	private String query_lastVerifyUser;
	private String query_note;
	private String query_name;
	private String query_icon;
	private String query_info;
	private String query_status;
	
	private String canApprove;
	
	private String canGobake;
	private String isShowPage;
	
	private Map statusMap;
	private Map optTypeMap;
	
	private String DATA_COLLECT_ID;
	private String NAME;
	private String TEL;
	private String ADDR;
	private String CITY;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.dataCollect = dataCollectService.getDataCollect(Long
					.parseLong(selectedId));
		}

		statusMap =  dataCollectService.getStatusMap(); 
		optTypeMap = dataCollectService.getOptTypeMap();
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		// queryMap.put("dataCollectId", query_dataCollectId);
		// queryMap.put("dataCollectBatchId", query_dataCollectBatchId);
		queryMap.put("batchNum", query_batchNum);
		queryMap.put("seq", query_seq);
		queryMap.put("link", query_link);
		queryMap.put("telType", query_telType);
		queryMap.put("tel", query_tel);
		queryMap.put("city", query_city);
		queryMap.put("class01", query_class01);
		queryMap.put("class02", query_class02);
		queryMap.put("addr", query_addr);
		queryMap.put("fax", query_fax);
		queryMap.put("tel02", query_tel02);
		queryMap.put("createTime", query_createTime);
		// queryMap.put("lastDataVerifyId", query_lastDataVerifyId);
		queryMap.put("lastVerifyTime", query_lastVerifyTime);
		queryMap.put("lastVerifyUser", query_lastVerifyUser);
		queryMap.put("note", query_note);
		queryMap.put("name", query_name);
		queryMap.put("icon", query_icon);
		queryMap.put("info", query_info);
		queryMap.put("status", query_status);

		resultList = dataCollectService.queryDataCollectByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		dataCollectService.saveDataCollect(dataCollect);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		
		if (dataCollect.getTel().length() == 0) {
			this.addActionError("** Tel 不能为空 !");
		}
		

		this.setValidatorResult("/omc/datacollect/DataCollectAdd.jsp");
	}

	public String toEdit() {		
		Long id = new Long(selectedId);
		dataCollect = dataCollectService.getDataCollect(id);
		if(dataCollect==null){
			if(Integer.parseInt(selectedId)==0){
				message = "没有找到对应上一页的信息!";
				isShowPage="NO";
			}else{
				message = "没有找到对应下一页的信息!";
				isShowPage="NO";
			}
			
		}else{
			if("PENDING".equals(dataCollect.getStatus())){
				canApprove = "YES";
			}else{
				canApprove = "NO";
			}
			List<Map> dataVerifyListMap=dataVerifyService.getApprovedDataByCollectId(selectedId);
			if(dataVerifyListMap.size()>0){
				for (Map map : dataVerifyListMap) {
					if("PENDING".equals(map.get("STATUS"))){
						canGobake = "YES";
						break;
					}else{
						canGobake = "NO";
					}
				}
				
			}else{
				canGobake = "NO";
			}
			
			
			
			Map toEditMap = new HashMap();
			toEditMap.put("id", selectedId);
			
			resultList = dataCollectService.toEditDataCollectById(toEditMap);
	
	//		dataCollect = dataCollectService.getDataCollect(id);
		}

		return RESULT_EDIT;
	}

	public String update() {		
		
		//dataCollect = dataCollectService.getDataCollect(Long.parseLong(DATA_COLLECT_ID));
		
		dataVerify = new DataVerify();

		dataVerify.setDataCollectId(Long.parseLong(DATA_COLLECT_ID));
		dataVerify.setName(NAME);
		dataVerify.setTel(TEL);
		dataVerify.setAddr(ADDR);
		dataVerify.setCity(CITY);
		dataVerify.setRegionCode(dataCollect.getRegionCode());
		dataVerify.setCreateTime(new Date());
		dataVerify.setCreateUser(loginUser.getUserId());
		dataVerify.setStatus("PENDING");
		dataVerify.setListId(dataCollect.getListId());
		dataVerify.setOptType(dataCollect.getOptType());

		dataVerifyService.saveDataVerify(dataVerify);
		
		dataCollect.setStatus("APPROVED");
		dataCollect.setLastVerifyTime(new Date());
		dataCollect.setLastVerifyUser(loginUser.getUserId());
		dataCollect.setLastDataVerifyId(dataVerify.getDataVerifyId());
		
	    dataCollectService.updateDataCollect(dataCollect);	

		

		message = "修改审核成功!";
		//return RESULT_EDIT;
		int id=Integer.parseInt(DATA_COLLECT_ID);
		selectedId=""+id;
		return toEdit();
	}
//设定审核不成功 zhongkaijian
public String updateIsNo() {		
		dataCollect.setStatus("DELETE");
		dataCollect.setLastVerifyTime(new Date());
		dataCollect.setLastVerifyUser(loginUser.getUserId());
	    dataCollectService.updateDataCollect(dataCollect);	
		message = "设置审核不通过成功!";
		int id=Integer.parseInt(DATA_COLLECT_ID);
		selectedId=""+id;
		return toEdit();
	}
//当审核通过后，但还没有入库的情况下，点击回退设置为未审核
public String updateIsPev() {		
	
	dataCollect = dataCollectService.getDataCollect(Long.parseLong(DATA_COLLECT_ID));
	
	dataVerify = new DataVerify();

	dataVerify.setDataVerifyId(dataCollect.getLastDataVerifyId());
	dataVerify.setDataCollectId(Long.parseLong(DATA_COLLECT_ID));
	dataVerify.setName(NAME);
	dataVerify.setTel(TEL);
	dataVerify.setAddr(ADDR);
	dataVerify.setCity(CITY);
	dataVerify.setRegionCode(dataCollect.getRegionCode());
	//dataVerify.setCreateTime();
	dataVerify.setLastUpdateTime(new Date());
	dataVerify.setLastUpdateUser(loginUser.getUserId());
	//dataVerify.setCreateUser();
	dataVerify.setStatus("DELETE");
	dataVerify.setListId(dataCollect.getListId());
	dataVerify.setOptType(dataCollect.getOptType());

	//dataVerifyService.saveDataVerify(dataVerify);
	dataVerifyService.updateDataVerify(dataVerify);
	
	dataCollect.setStatus("PENDING");
	dataCollect.setLastVerifyTime(new Date());
	dataCollect.setLastVerifyUser(loginUser.getUserId());
	//dataCollect.setLastDataVerifyId(dataVerify.getDataVerifyId());
	
    dataCollectService.updateDataCollect(dataCollect);	

	

	message = "设置回退成功!";
	//return RESULT_EDIT;
	int id=Integer.parseInt(DATA_COLLECT_ID);
	selectedId=""+id;
	return toEdit();
}
	public String toApproved() {		
		Long id = new Long(selectedId);

		dataCollect = dataCollectService.getDataCollect(id);
		
		dataVerify =  dataVerifyService.getDataVerifyByCollectId(selectedId);
		
		
		Map toEditMap = new HashMap();
		toEditMap.put("id", selectedId);
		
		resultList = dataVerifyService.getApprovedDataByCollectId(selectedId);

//		dataCollect = dataCollectService.getDataCollect(id);

		return RESULT_APPROVED;
	}

	public void validateUpdate() {
//		if (TEL.length() == 0) {
//			this.addActionError("** 电话不能为空 !");
//		}
		if (NAME.length() == 0) {
			this.addActionError("** 商户名称不能为空 !");
		}
		
		this.setValidatorResult("/omc/datacollect/DataCollectUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.dataCollectService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setDataCollect(DataCollect dataCollect) {
		this.dataCollect = dataCollect;
	}

	public DataCollect getDataCollect() {
		return dataCollect;
	}

	public void setDataCollectService(DataCollectService dataCollectService) {
		this.dataCollectService = dataCollectService;
	}

	public List<DataCollect> getDataCollectList() {
		return dataCollectList;
	}

	public void setDataCollectList(List<DataCollect> dataCollectList) {
		this.dataCollectList = dataCollectList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_dataCollectId() {
		return this.query_dataCollectId;
	}

	public void setQuery_dataCollectId(String dataCollectId) {
		this.query_dataCollectId = dataCollectId;
	}

	public String getQuery_dataCollectBatchId() {
		return this.query_dataCollectBatchId;
	}

	public void setQuery_dataCollectBatch(String dataCollectBatchId) {
		this.query_dataCollectBatchId = dataCollectBatchId;
	}

	public String getQuery_seq() {
		return this.query_seq;
	}

	public void setQuery_seq(String seq) {
		this.query_seq = seq;
	}

	public String getQuery_link() {
		return this.query_link;
	}

	public void setQuery_link(String link) {
		this.query_link = link;
	}

	public String getQuery_telType() {
		return this.query_telType;
	}

	public void setQuery_telType(String telType) {
		this.query_telType = telType;
	}

	public String getQuery_tel() {
		return this.query_tel;
	}

	public void setQuery_tel(String tel) {
		this.query_tel = tel;
	}

	public String getQuery_city() {
		return this.query_city;
	}

	public void setQuery_city(String city) {
		this.query_city = city;
	}

	public String getQuery_class01() {
		return this.query_class01;
	}

	public void setQuery_class01(String class01) {
		this.query_class01 = class01;
	}

	public String getQuery_class02() {
		return this.query_class02;
	}

	public void setQuery_class02(String class02) {
		this.query_class02 = class02;
	}

	public String getQuery_addr() {
		return this.query_addr;
	}

	public void setQuery_addr(String addr) {
		this.query_addr = addr;
	}

	public String getQuery_fax() {
		return this.query_fax;
	}

	public void setQuery_fax(String fax) {
		this.query_fax = fax;
	}

	public String getQuery_tel02() {
		return this.query_tel02;
	}

	public void setQuery_tel02(String tel02) {
		this.query_tel02 = tel02;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastDataVerifyId() {
		return this.query_lastDataVerifyId;
	}

	public void setQuery_lastDataVerifyId(String lastDataVerifyId) {
		this.query_lastDataVerifyId = lastDataVerifyId;
	}

	public String getQuery_lastVerifyTime() {
		return this.query_lastVerifyTime;
	}

	public void setQuery_lastVerifyTime(String lastVerifyTime) {
		this.query_lastVerifyTime = lastVerifyTime;
	}

	public String getQuery_lastVerifyUser() {
		return this.query_lastVerifyUser;
	}

	public void setQuery_lastVerifyUser(String lastVerifyUser) {
		this.query_lastVerifyUser = lastVerifyUser;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_icon() {
		return this.query_icon;
	}

	public void setQuery_icon(String icon) {
		this.query_icon = icon;
	}

	public String getQuery_info() {
		return this.query_info;
	}

	public void setQuery_info(String info) {
		this.query_info = info;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public String getQuery_batchNum() {
		return query_batchNum;
	}

	public void setQuery_batchNum(String queryBatchNum) {
		query_batchNum = queryBatchNum;
	}

	public void setQuery_dataCollectBatchId(String queryDataCollectBatchId) {
		query_dataCollectBatchId = queryDataCollectBatchId;
	}

	public Map getStatusMap() {
		return statusMap;
	}

	public void setStatusMap(Map statusMap) {
		this.statusMap = statusMap;
	}

	public String getNAME() {
		return NAME;
	}

	public void setNAME(String nAME) {
		NAME = nAME;
	}

	public void setDataVerifyService(DataVerifyService dataVerifyService) {
		this.dataVerifyService = dataVerifyService;
	}

	public String getTEL() {
		return TEL;
	}

	public void setTEL(String tEL) {
		TEL = tEL;
	}

	public String getADDR() {
		return ADDR;
	}

	public void setADDR(String aDDR) {
		ADDR = aDDR;
	}

	public String getDATA_COLLECT_ID() {
		return DATA_COLLECT_ID;
	}

	public void setDATA_COLLECT_ID(String dATACOLLECTID) {
		DATA_COLLECT_ID = dATACOLLECTID;
	}
	
	public String getCanApprove() {
		return canApprove;
	}

	public void setCanApprove(String canApprove) {
		this.canApprove = canApprove;
	}

	public DataVerify getDataVerify() {
		return dataVerify;
	}

	public void setDataVerify(DataVerify dataVerify) {
		this.dataVerify = dataVerify;
	}

	public String getCITY() {
		return CITY;
	}

	public void setCITY(String cITY) {
		CITY = cITY;
	}

	public Map getOptTypeMap() {
		return optTypeMap;
	}

	public void setOptTypeMap(Map optTypeMap) {
		this.optTypeMap = optTypeMap;
	}

	public String getCanGobake() {
		return canGobake;
	}

	public void setCanGobake(String canGobake) {
		this.canGobake = canGobake;
	}

	/**
	 * @return the isShowPage
	 */
	public String getIsShowPage() {
		return isShowPage;
	}

	/**
	 * @param isShowPage the isShowPage to set
	 */
	public void setIsShowPage(String isShowPage) {
		this.isShowPage = isShowPage;
	}

}
